<template>
  <div class="app-container">
    <div v-show="!detailInfo.visible">
      <!-- 条件栏 -->
      <page-filter
        v-model:query="filterInfo.query"
        :filter-list="filterInfo.list"
      >
        <template v-slot:filter-confirm>
          <el-button :disabled="!canConfirm" @click="confirm()">确认</el-button>
        </template>
        <template v-slot:filter-refuse>
          <el-button :disabled="!canRefuse" @click="refuse()">拒绝</el-button>
        </template>
        <template v-slot:filter-signDate>
          <el-date-picker
            @change="timeChange"
            v-model="time"
            format="YYYY-MM-DD HH:mm:ss"
            value-format="YYYY-MM-DD HH:mm:ss"
            type="datetimerange"
            range-separator="至"
            :shortcuts="$fn.pickerOptionsFn()"
            align="right"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :default-time="[
              new Date(2000, 1, 1, 0, 0, 0),
              new Date(2000, 1, 1, 23, 59, 59),
            ]"
          ></el-date-picker>
        </template>
      </page-filter>
      <!-- 表格 -->
      <table-container
        :refresh="tableInfo.refresh"
        :init-curpage="tableInfo.initCurpage"
        v-model:data="tableInfo.data"
        :api="tableInfo.api"
        :query="filterInfo.query"
        :field-list="tableInfo.fieldList"
        :handle="tableInfo.handle"
        @handleEvent="handleEvent"
      >
        <template v-slot:col-code="scope">
          <span class="slip-edit" @click="openDetail(scope.row)">{{
            scope.row.code
          }}</span>
        </template>
        <template v-slot:col-statusValue="scope">
          <span>{{
            scope.row.orgId == orgId ? '待对方审核' : '待我审核'
          }}</span>
        </template>
        <template v-slot:col-updateUserName="scope">
          <span v-if="scope.row.status === 2 || scope.row.status === 3">{{
            scope.row.updateUserName
          }}</span>
          <span v-else>-</span>
        </template>
      </table-container>
    </div>

    <pend-check-brand-detail
      v-if="detailInfo.visible"
      v-model:visible="detailInfo.visible"
      :id="detailInfo.id"
    />
  </div>
</template>

<script setup name="pendCheckContract">
import PageFilter from '@/components/PageFilter/index'
import TableContainer from '@/components/TableContainer/index'
import { getContractPage, contractConfirm, contractRefuse } from '@/api/core'
import PendCheckBrandDetail from './detail'
import moment from 'moment'

const { proxy } = getCurrentInstance()
const emit = defineEmits()

// 过滤相关配置
const orgId = ref(proxy.$store.state.user.userInfo.user.orgId)
const time = ref([])
const filterInfo = reactive({
  query: {
    sort: 'create_time desc',
    status: 1,
    partyA: proxy.$store.state.user.userInfo.user.orgId,
  },
  list: [
    {
      type: 'slot',
      value: 'confirm',
      label: '确认',
      show: proxy.$permission('pendCheckContract:audit'),
    },
    {
      type: 'slot',
      value: 'refuse',
      label: '拒绝',
      show: proxy.$permission('pendCheckContract:refuse'),
    },
    {
      type: 'select-model',
      code: 'license_check_status',
      clearable: true,
      label: '状态',
      value: 'status',
      event: search,
    },
    { label: '签订日期', value: 'signDate', type: 'slot', event: search },
    {
      type: 'input',
      value: 'keyword',
      label: '名称/编码',
      event: search,
    },
    { type: 'button', label: '搜索', event: search },
  ],
})

// 表格相关
const tableInfo = reactive({
  refresh: 1,
  initCurpage: 1,
  api: getContractPage,
  data: [],
  selections: [],
  fieldList: [
    { label: '合同编号', value: 'code', type: 'slot' },
    { label: '合同名称', value: 'name' },
    { label: '供应商', value: 'partyBName' },
    {
      label: '合同金额',
      value: 'sum',
      type: 'column',
      formatter: proxy.$fn.TripartiteMethod,
    },
    { label: '签订日期', value: 'signDate' },
    { label: '截止日期', value: 'expirationDate' },
    { label: '合同状态', value: 'statusValue', type: 'slot' },
    { label: '审核人', value: 'updateUserName', type: 'slot' },
    { label: '备注', value: 'remark' },
  ],
  handle: {
    fixed: 'right',
    label: '操作',
    width: '100',
    btList: [
      {
        label: '确认',
        event: confirm,
        show: proxy.$permission('pendCheckContract:confirm'),
        ifRender: (row) => row.status === 1 && row.orgId != orgId.value,
      },
      {
        label: '拒绝',
        event: refuse,
        show: proxy.$permission('pendCheckContract:refuse'),
        ifRender: (row) => row.status === 1 && row.orgId != orgId.value,
      },
    ],
  },
})
const detailInfo = reactive({
  visible: false,
  id: '',
})

const canConfirm = computed(() => {
  return (
    tableInfo.selections.length &&
    tableInfo.selections.filter((value) => value.status !== 1).length === 0
  )
})
const canRefuse = computed(() => {
  return (
    tableInfo.selections.length &&
    tableInfo.selections.filter((value) => value.status !== 1).length === 0
  )
})

watch(
  () => filterInfo.query.updateTimeGreater,
  (val) => {
    if (!val) {
      time.value = null
    }
  }
)

onMounted(() => {
  getList()
})

// 获取列表
function getList() {
  tableInfo.refresh = Math.random()
}
// 搜索
function search() {
  // 重置分页
  tableInfo.initCurpage = Math.random()
  tableInfo.refresh = Math.random()
}
// 触发事件
function handleEvent(event, data) {
  switch (event) {
    case 'tableCheck':
      tableInfo.selections = [...data]
      break
  }
}
//确认
function confirm(row = undefined) {
  const ids = getIds(row)
  contractConfirm(ids).then((resp) => {
    if (resp && resp.success) {
      getList()
    }
  })
}
//拒绝
function refuse(row = undefined) {
  const ids = getIds(row)
  contractRefuse(ids).then((resp) => {
    if (resp && resp.success) {
      getList()
    }
  })
}
function getIds(row) {
  if (row) return [row.id]
  else return tableInfo.selections.map((value) => value.id)
}
function openDetail(row) {
  detailInfo.id = row.id
  detailInfo.visible = true
}
function timeChange(val) {
  if (val && val.length) {
    filterInfo.query.updateTimeGreater = val[0]
    filterInfo.query.updateTimeLess = val[1]
  } else {
    filterInfo.query.updateTimeGreater = ''
    filterInfo.query.updateTimeLess = ''
  }
}
</script>

<style scoped></style>
